﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WinAutomator.Library;
using WinAutomator.Library.InOut;
using System.IO;

namespace WinAutomator.Plugin.Files.ReadDirectory
{
    class ReadDirectoryProcess : IProcess
    {
        public String Directory { get; set; }
        public String Extension { get; set; }
        public bool Recursive { get; set; }
        public bool Hidden { get; set; }

        IConnector _nextConnector;
        List<VirtualConnector> _vconnector = new List<VirtualConnector>();

        public VirtualConnector VIn { get; protected set; }
        public VirtualConnector VOut { get; protected set; }

        private Connector<WinAutomator.Library.MainType.WAAllType> In { get; set; }
        private Connector<List<String>> Out { get; set; }

        public ReadDirectoryProcess()
        {
            In = new Connector<WinAutomator.Library.MainType.WAAllType>(WAInOut.In, this);
            Out = new Connector<List<String>>(WAInOut.Out, this);
            Out.GetProcessData = getResult;

            VIn = new VirtualConnector(WAInOut.In);
            VIn.AddConnector(In);
            _vconnector.Add(VIn);

            VOut = new VirtualConnector(WAInOut.Out);
            VOut.AddConnector(Out);
            _vconnector.Add(VOut);

            Recursive = false;
            Hidden = false;
            Extension = String.Empty;
        }

        public List<String> Result { get; private set; }

        public List<String> getResult()
        {
            return Result;
        }

        public IConnector Execute()
        {
            if ((String.IsNullOrEmpty(Extension)) && (!Recursive))
                Result = System.IO.Directory.GetFiles(Directory).ToList();
            if ((String.IsNullOrEmpty(Extension)) && (Recursive))
                Result = System.IO.Directory.GetFiles(Directory, "*.*", SearchOption.AllDirectories).ToList();
            if ((!String.IsNullOrEmpty(Extension)) && (!Recursive))
                Result = System.IO.Directory.GetFiles(Directory, Extension).ToList();
            if ((!String.IsNullOrEmpty(Extension)) && (Recursive))
                Result = System.IO.Directory.GetFiles(Directory, Extension, SearchOption.AllDirectories).ToList();

            _nextConnector = (IConnector)Out;

            return _nextConnector;

        }

        public List<Type> getAcceptedType()
        {
            List<Type> type = new List<Type>() { typeof(object) };

            return type;
        }

        public List<Library.InOut.VirtualConnector> getVirtualConnectors()
        {
            return _vconnector;
        }

        public List<Library.InOut.VirtualConnector> getVirtualConnectors(Library.InOut.WAInOut type)
        {
            List<VirtualConnector> tmp = new List<VirtualConnector>();

            foreach (VirtualConnector v in _vconnector)
                if (v.VirtualConnectorType == type)
                    tmp.Add(v);

            return tmp;
        }
    }
}
